package com.sky.web.admin;

import com.sky.context.ThreadLocalUtil;
import com.sky.result.PageResult;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody EmployeeDTO employeeDTO) {
        Employee e = employeeService.login(employeeDTO);
        //登录后获取jwt令牌
        Map<String, Object> c = new HashMap<>();
        c.put("empId", e.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecret(),
                jwtProperties.getAdminTtl(),
                c
        );
        //返回前端所需数据对象
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(e.getId())
                .userName(e.getUsername())
                .name(e.getName())
                .token(token)
                .build();
        return Result.success(employeeLoginVO);
    }

    //退出 前端响应200即可
    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }

    //添加员工 DTO
    @PostMapping
    public Result add(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.add(employeeDTO);
        return Result.success();
    }

    //分页条件查询
    @GetMapping("/page")
    public Result findByPage(String name,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult pageResult = employeeService.findByPage(name, page, pageSize);
        return Result.success(pageResult);

    }

    //信息回显
    @GetMapping("/{id}")
    public Result fainById(@PathVariable("id") Long id) {
        Employee employee = employeeService.findById(id);
        return Result.success(employee);

    }

    //修改员工信息
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.update(employeeDTO);
        return Result.success();

    }

    //启用禁用账号的功能
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,
                              @RequestParam Long id) {
        employeeService.startOrStop(status, id);
        return Result.success();
    }

    //修改密码
    @PutMapping("/editPassword")
    public Result updatePassword(@RequestBody EmployeeDTO employeeDTO) {
        Long id = ThreadLocalUtil.getCurrentId();
        employeeService.updatedPassword(id, employeeDTO);
        return Result.success();
    }
}